package com.example.flowable.utils;

import com.example.db.SqlBuilder;
import com.example.flowable.model.TableModel;
import com.example.flowable.model.TableModelConfig;
import com.example.flowable.model.enummodel.TableFieldTypeEnum;
import com.example.flowable.model.vo.TableModelVo;

import java.util.List;
import java.util.StringJoiner;

/**
 * @Classname TableSqlUtils
 * @Description TODO
 * @Date 2022/8/16 23:38
 * @Created by XiongXiong
 * @Author: X.I.O
 */
public class TableSqlUtils {

    String base="CREATE TABLE `sys_test`  (\n" +
            "  `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '注释111',\n" +
            "  `long11` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '注释22222',\n" +
            "  `www` datetime(0) NULL DEFAULT NULL COMMENT '注释333',\n" +
            "  `rq` date NULL DEFAULT NULL COMMENT '注释444444',\n" +
            "  `eee` decimal(65, 0) NULL DEFAULT NULL,\n" +
            "  `we` double(10, 0) NULL DEFAULT NULL,\n" +
            "  `1111` float(10, 0) NULL DEFAULT NULL,\n" +
            "  `222222` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,\n" +
            "  `666666` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '3333333333',\n" +
            "  `createdate` datetime(0) NULL DEFAULT NULL,\n" +
            "  `isdel` tinyint(1) NULL DEFAULT NULL COMMENT '删除',\n" +
            "  `updatedate` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',\n" +
            "  `bindid` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '业务id',\n" +
            "  PRIMARY KEY (`id`, `222222`, `666666`) USING BTREE\n" +
            ") ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;";

    private static final String CHARSTYPE="CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci";

    private static String getBase(String tableName,String string){
        StringBuilder stringBuilder=new StringBuilder();
        String common=getCommonSql();
        stringBuilder.append(" CREATE TABLE `").append(tableName)
                .append("` ( ");
        stringBuilder.append(string);
        stringBuilder.append(",");
        stringBuilder.append(common)
                .append(" ) ");
        return stringBuilder.toString();
    }

    public static String createTable(TableModelVo tableModelVo){
        if(tableModelVo==null){
            return null;
        }
        StringJoiner stringJoiner=new StringJoiner(",");
        List<TableModelConfig> tableModelConfigs=tableModelVo.getTableModelConfigs();
        for (int i = 0; i < tableModelConfigs.size() ; i++) {
            TableModelConfig tableModelConfig=tableModelConfigs.get(i);
            String str=getFieldString(tableModelConfig);
            stringJoiner.add(str);
        }
        String fieldSql=stringJoiner.toString();
        if("".equals(fieldSql)){
            return null;
        }
       String createSql=getBase(tableModelVo.getTableName(),fieldSql);
        return createSql;
    }

    private static String getFieldString(TableModelConfig tableModelConfig){
        if (tableModelConfig == null) {
            return null;
        }
        boolean isPoint=false;
        if(tableModelConfig.getPointLength()!=null){
            isPoint=true;
        }
        return getFieldString(
                tableModelConfig.getFieldName(),
                tableModelConfig.getFieldType(),
                tableModelConfig.getFieldLength(),
                isPoint,
                tableModelConfig.getPointLength(),
                tableModelConfig.isNull(),
                tableModelConfig.getFieldDesc()
        );
    }

    /**
     *
     * @param fieldName 字段名称
     * @param fieldType  字段类型
     * @param length      长度
     * @param ispoint     是否小数点
     * @param poinLength  小数点
     * @param isNull     是否null
     * @param comment    备注
     * @return
     */
    private static String getFieldString(String fieldName,String fieldType,Integer length,boolean ispoint,Integer poinLength,
                                         boolean isNull,String comment){
        String charsString=null;
        if(fieldType.equals(TableFieldTypeEnum.VQRCHAR.getFieldType())){
            charsString=CHARSTYPE;
        }
        return getFieldString(fieldName,fieldType,length,ispoint,poinLength,isNull,comment,charsString);
    }

    /**
     *
     * @param fieldName 字段名称
     * @param fieldType  字段类型
     * @param length      长度
     * @param ispoint     是否小数点
     * @param poinLength  小数点
     * @param isNull     是否null
     * @param comment    备注
     * @param characters   字符
     * @return
     */
    private static String getFieldString(String fieldName,String fieldType,Integer length,boolean ispoint,Integer poinLength,
                                         boolean isNull,String comment,String characters){
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append(" `").append(fieldName).append("` ");
        stringBuilder.append(fieldType);
        if(!fieldType.equals(TableFieldTypeEnum.LONGTEXT.getFieldType())){
            //长文本没有长度
            stringBuilder.append("(").append(length);
            if(ispoint && poinLength != null && poinLength > 0){
                stringBuilder.append(",").append(poinLength);
            }
            stringBuilder.append(") ");
            if(characters!=null){
                stringBuilder.append(characters);
            }
        }
        if(isNull){
            stringBuilder.append(" NOT NULL ");
        }else {
            stringBuilder.append(" NULL DEFAULT NULL ");
        }
        stringBuilder.append(" COMMENT ").append("'").append(comment).append("'");
        return stringBuilder.toString();
    }

    private static String getCommonSql(){
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append(" `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键id' ,");
        stringBuilder.append(" `isdel` tinyint(1) NULL DEFAULT NULL COMMENT '删除', ");
        stringBuilder.append(" `createdate` datetime(0) NULL DEFAULT NULL COMMENT '创建时间', ");
        stringBuilder.append(" `updatedate` datetime(0) NULL DEFAULT NULL COMMENT '更新时间', ");
        stringBuilder.append(" `processModelId` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '流程模型id', ");
        stringBuilder.append(" `processdefid` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '流程版本id', ");
        stringBuilder.append(" `processTitle` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '流程业务标题', ");
        stringBuilder.append(" `bindid` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '业务id', ");
        stringBuilder.append(" PRIMARY KEY (`id`) USING BTREE ");
        return stringBuilder.toString();
    }

    /**
     * 添加字段
     * @param tableName 表名
     * @param fieldName 字段名
     * @param fieldType  字段类型
     * @param length    字段长度
     * @param point      小数点
     * @param comment    注释
     * @return
     */
    private static String addField(String tableName,String fieldName,String fieldType,Integer length,Integer point,String comment){
        //String s="ALTER TABLE student ADD age INT(4);";
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append("ALTER TABLE ").append(tableName).append(" ADD ")
        .append(fieldName).append(" ").append(fieldType).append(" ").append("(").append(length);
        if(point != null){
            stringBuilder.append(",").append(point);
        }
        stringBuilder.append(") COMMENT ").append(comment);
        return stringBuilder.toString();
    }

    /**
     * 删除字段
     * @param tableName 表名
     * @param fieldName  字段名
     * @return
     */
    private static String deleteField(String tableName,String fieldName){
        //String s="ALTER TABLE <表名> DROP <字段名>；";
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(" ALTER TABLE ").append(tableName).append(" DROP ").append(fieldName);
        return stringBuilder.toString();
    }


}
